### Project 12 Bluetooth Controlled Smart Car **1.Principle** In the previous section, you have learned the principles of Bluetooth and how to use Bluetooth to control a small light. Okay, based on that, can we use Bluetooth to send a command to control the movement of a smart car? Absolutely yeah, in the previous section, we can use a mobile APP to send a character. The ARDUINO board can use a Bluetooth module to receive the Bluetooth signal from the mobile phone, and feed it back to the main control board. Then main control board will analyze and judge the collected signals. If correct, it will control the movement of the smart car. Here we don't need a Bluetooth serial assistant as mentionedabove. We use an Android APP developed by our keyestudio team to control the smart car. **Please download the app here**![](media/image-20251218173017243.png). ![](media/image-20251219102854973.png) The interface of this APP is very simple, as shown below. ![](media/image-20251218173742071.png) Connected the Bluetooth, let’s make use of a little program that can read the serial data, to check what character the five buttons send. So that we can apply it to the example code of Bluetooth smart car in the following projects. Code 15 ```c char val;// define the variable val void setup() { Serial.begin(9600);// set the baud rate as 9600, the same as software setting. When connecting the particular device like Bluetooth, it should be consistent with the baud rate of other devices. } void loop() { val=Serial.read();//read the data received from serial port, and assign it to val Serial.println(val);// print val data delay(300);//delay 0.3S } ``` Through the above program, we can get that the five buttons are Upward (“U”), Downward (“D”), Left (“L”), Right (“R”), and Stop (“S”). The principle is very simple. When Bluetooth module receives these characters sent by the mobile phone, and then it will send them to ARDUINO. ARDUINO will control the rotation direction of motor according to the preset value in the code. When receive the information "U", the smart car will move forward. When receive "D", the smart car goes backward. If receive "L", turn left. If receive "R", turn right. The smart car stops when it receives the "S". As shown below, connect the components to test it. ![](media/image-20251218173859913.png) Notice: the Bluetooth module is directly plugged into the shield.  After wiring, you can get the project code as follows: Code 16 ```c int E1 = 9; // set the speed pin of motor A as D9 int E2 = 5; // set the speed pin of motor B as D5 int M1 = 2; // set the direction pin of motor A as D2 int M2 = 4; // set the direction pin of motor B as D4 int val;// set the phone APP data received by Bluetooth void setup(void) { Serial.begin(9600); pinMode(M1,OUTPUT); // set M1 as OUTPUT mode pinMode(M2,OUTPUT); // set M2 as OUTPUT mode pinMode(E1,OUTPUT); // set E1 as OUTPUT mode pinMode(E2,OUTPUT); // set E2as OUTPUT mode stopp(); } void advance(void) // set the forward motion { digitalWrite(M1,HIGH); // motor A turns forward, wheels go forward. digitalWrite(M2,HIGH); // motor B turns forward, wheels go forward. analogWrite(E1,250); // speed of motor A(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) analogWrite(E2,250); // speed of motor B(can be adjusted according to the actual speed of motor. Turn up the value to accelerate, lower the value to decelerate.) } void back(void) // set the backward motion { digitalWrite(M1,LOW); // motor A turns reverse, wheels go backward. digitalWrite(M2, LOW); // motor B turns reverse, wheels go backward. analogWrite(E1,250); // speed of motor A analogWrite(E2,250); // speed of motor B } void turnL(void) // set the left turn motion { digitalWrite(M1,LOW); // motor A turns reverse, wheels go backward. digitalWrite(M2, HIGH); // motor B turns forward, wheels go forward, smart car will turn left. analogWrite(E1,250); // speed of motor A analogWrite(E2,250); // speed of motor B } void turnR(void) // set the right turn motion { digitalWrite(M1,HIGH); // motor A turns forward, wheels go forward. digitalWrite(M2,LOW); // motor B turns reverse, wheels go backward, smart car will turn right. analogWrite(E1,250); // speed of motor A analogWrite(E2,250); // speed of motor B } void stopp(void) // set the Stop motion { digitalWrite(M1,LOW); // motor A turns reverse digitalWrite(M2, LOW); // motor B turns reverse analogWrite(E1, 0); // speed of motor A, speed as zero, means Stop. analogWrite(E2, 0); // speed of motor B, speed as zero, means Stop. } void loop() { val=Serial.read(); // Bluetooth module reads the data sent by phone APP if(val=='U') advance();// Bluetooth module receives the character U , smart car goes forward. if(val=='D') back();// Bluetooth module receives the character D, smart car goes backward. if(val=='L') turnL();// Bluetooth module receives the character L, smart car turns left. if(val=='R') turnR();// Bluetooth module receives the character R, smart car turns right . if(val=='S') stopp();// Bluetooth module receives the character S, smart car stops. } ``` **2.Example Result** Done uploading the above code to control board, open APP, connect the Bluetooth, you should see the LED on the module is always on. Press down any buttons on APP, you can control the smart car running.